package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.pojo.SysDictData;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xuchangshuo
 * @date 2020/8/4 10:03
 */
@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Override
    public int getSysDictDataCount(String dict_type_uid) {
        int count = sysDictDataMapper.getSysDictDataCount(dict_type_uid);
        return count;
    }

    @Override
    public void add(@RequestBody Map<String, Object> map) {
        SysDictData sysDictData = remove(map);
        sysDictDataMapper.insertSelective(sysDictData);
    }

    @Override
    public void deleteBatch(List<Map<String, Object>> list) {
        //创建一个集合 接受map中的uid
        ArrayList<String> uids = new ArrayList<>();
        for (Map<String, Object> map : list) {
            String uid = (String) map.get("uid");
            uids.add(uid);
        }
        for (String uid : uids) {
            Example example = new Example(SysDictData.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("uid", uid);
            sysDictDataMapper.deleteByExample(example);
        }
    }

    @Override
    public void edit(@RequestBody Map<String, Object> map) {
        SysDictData sysDictData = remove(map);
        sysDictDataMapper.updateByPrimaryKeySelective(sysDictData);
    }

    @Override
    public Page<SysDictData> getList(@RequestBody Map<String, Object> map) {
        Integer pageSize = (Integer) map.get("pageSize");
        Integer currentPage = (Integer) map.get("currentPage");
        PageHelper.startPage(currentPage, pageSize);
        Page<SysDictData> dictDataList = (Page<SysDictData>) sysDictDataMapper.selectAll();
        return dictDataList;
    }

    @Override
    public List<SysDictData> getListByDictType(String dictType) {
        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dictType", dictType);
        List<SysDictType> sysDictTypeList = sysDictTypeMapper.selectByExample(example);

        List<SysDictData> dictDataList = null;
        for (SysDictType sysDictType : sysDictTypeList) {
            dictDataList = sysDictDataMapper.getListByDictType(sysDictType.getUid());
        }
        return dictDataList;
    }

    @Override
    public List<SysDictData> getListByDictTypeList(String[] dictTypeList) {

        ArrayList<SysDictData> list = new ArrayList<>();
        if (dictTypeList == null) {
            throw new RuntimeException("字典为空！");
        }
        for (String dictType : dictTypeList) {
            Example example = new Example(SysDictType.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("dictType", dictType);
            List<SysDictType> sysDictTypeList = sysDictTypeMapper.selectByExample(example);
            List<SysDictData> DataList = null;
            for (SysDictType sysDictType : sysDictTypeList) {
                DataList = sysDictDataMapper.getListByDictType(sysDictType.getUid());
            }
            for (SysDictData dictData : DataList) {
                list.add(dictData);
            }
        }
        return list;

    }


    //对传入的map集合进行处理
    public SysDictData remove(Map<String, Object> map) {
        map.remove("keyword");
        map.remove("pageSize");
        map.remove("currentPage");
        //将map转为json字符串
        String jsonString = JSON.toJSONString(map);
        SysDictData sysDictData = JSON.parseObject(jsonString, SysDictData.class);

        return sysDictData;
    }

}
